set more 1
use ..\data\paper_data.dta, clear
drop if treatment_id==3
xi i.treatment_id*i.part, noomit
global reps=9999
global seed=96941726 // random number
global treatvars _Itreatment_1 _Itreatment_2

preserve

* Result 1
*=========

*Indeed, the estimated difference-in-difference coefficient is small and statistically insignificant ($\beta=-0.468$ and $p=0.535$ according to a wild-bootstrapped linear regression).
*====================================

bysort team_id (period): gen economy_id_n6=economy_id[_N]
collapse g _Itreatment_1 _Itreatment_2 _Ipart_3 _ItreXpar_2_3 if treatment_id>0, by(treatment_id economy_id economy_id_n6 part period)
reg g _Itreatment_1 _Itreatment_2 _Ipart_3 _ItreXpar_2_3
boottest {_ItreXpar_2_3}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)

restore
preserve

* Result 2
*=========

*Of the 53 cases where at least two offers to $H$ teams were made in Decentralized, in 36\% of them, only one of the two $H$ teams was targeted.
*===============================================================================================================================================

collapse g_team request_team target_team if part==3 & treatment_id==1, by(treatment_id economy_id period team_id gstar)

gen offer_to_h1_a=target_team==5
replace offer_to_h1_a=. if target_team==.
bys economy_id period (gstar): egen offer_to_h1=max(offer_to_h1_a)
bys economy_id period (gstar): egen n_offer_to_h1=sum(offer_to_h1_a)
gen offer_to_h2_a=target_team==6
replace offer_to_h2_a=. if target_team==. 
bys economy_id period (gstar): egen offer_to_h2=max(offer_to_h2_a)
bys economy_id period (gstar): egen n_offer_to_h2=sum(offer_to_h2_a)

collapse n_* g_team, by(treatment_id economy_id period)

bys treatment_id: tab n_* if n_offer_to_h1+n_offer_to_h2>1

restore
preserve

* Result 4
*=========

keep if part==2 & (treatment_id==1 | treatment_id==2)
collapse (mean)gstar amount_team request_team sign_team gstar_opponent offer_taken if high_gstar==0 & offer_made==1, by(treatment_id economy_id team_id period economy_type)

* Under $N=2$, the overwhelming majority of offers targeting teams with the highest ideal point was profitable for the sender (88\% in Decentralized and 89\% in Structured).
replace request_team=request_team-1 if sign_team==-1
replace request_team=gstar_opponent if sign_team==1 & request_team<gstar_opponent
gen profitable_sender=-amount_team-10*abs(gstar-request_team)>=-10*abs(gstar-gstar_opponent)
tab profitable_sender treatment_id, col

* Mutually beneficial offers represented 21\% of the total number of offers in Decentralized and 76\% of the total in Structured.
gen profitable_recipient=-4*request_team-10*abs(gstar_opponent-request_team)+amount_team>-4*gstar_opponent
gen profitable_both=(profitable_recipient+profitable_sender==2)
tab profitable_both treatment_id, col

* Result 5
*=========

*In the Decentralized treatment, 58\% of the profitable offers to the highest-ideal point teams were taken (11 out of 19). In the Structured treatment, instead, 82\% of the profitable offers were taken (66 out of 81).
bysort treatment_id: tab profitable_recipient offer_taken, row

*If we focus on mutually profitable offers, patterns remain similar: 43\% of these offers were taken in Structured (6 out of 14) and 81\% in Decentralized (62 out of 77).
bysort treatment_id: tab profitable_both offer_taken, row

*Finally, receivers rarely took offers that were unprofitable for them (17\% in Decentralized and 29\% in Structured).
*cf line 70

restore

* 4.3 Evidence from the chat
*===========================

use ..\data\chat.dta, clear

bysort subject_id (period): gen economy_id_n6=economy_id[_N]

gen time2=120-time if part==1
replace time2=60-time if part==2
replace time2=120-time if part==2 & treatment_id> 0 & effort_stage==0
replace time2=120-time if part==3 & treatment_id==0
replace time2=60-time  if part==3 & treatment_id==0 & period>18
replace time2=120-time if part==3 & treatment_id> 0 & effort_stage==0
replace time2=60-time  if part==3 & treatment_id> 0 & effort_stage==1
replace time2=60-time  if part==3 & treatment_id> 0 & effort_stage==0 & period>18
replace time2=30-time  if part==3 & treatment_id> 0 & effort_stage==1 & period>18

gen words=wordcount(text)
gen words_1=wordcount(text) if effort_stage==1 & time2<=60 & (period==5 | (period==18 & treatment_id==0))
replace words_1=wordcount(text) if effort_stage==1 & time2<=30 & (period==18 & treatment_id>0)
gen words_2=wordcount(text) if effort_stage==1 & time2 >60 & (period==5 | (period==18 & treatment_id==0))
replace words_2=wordcount(text) if effort_stage==1 & time2>30 & (period==18 & treatment_id>0)
gen words_1_offer=wordcount(text) if effort_stage==0 & time2<=60 & period==18 & treatment_id>0
gen words_2_offer=wordcount(text) if effort_stage==0 & time2 >60 & period==18 & treatment_id>0

preserve

* First, in Part 1, where all teams played Baseline with $N=2$, the chat volume is similar across treatments
collapse (sum)words if part==1, by(treatment_id economy_id period)
collapse (mean)words, by(treatment_id economy_id)
ranksum words if treatment_id!=2, by(treatment_id)
ranksum words if treatment_id!=1, by(treatment_id)
ranksum words if treatment_id!=0, by(treatment_id)

restore 
preserve 

* Second, in Baseline, where teams played under $N=2$ for five additional rounds compared to the other treatments, the chat activity significantly declined with experience
collapse (sum)words words_1 if treatment_id==0 & part<3, by(economy_id part period) // economy-part level dataset
gen share_1=words_1/words
sum share_1 if period==5
gen share_1_p5=r(mean) // no one says anything during the second min of period 5
gen words_share=words
replace words_share=words/share_1_p5 if part==2
gen words_pt1=words_share if part==1
gen words_pt2=words_share if part==2
collapse (mean)words_pt*, by(economy_id part) // economy-part level dataset
replace words_pt1=. if part==2
replace words_pt2=. if part==1 
collapse (mean)words_pt*, by(economy_id) // economy-part level dataset
signrank words_pt1=words_pt2

restore 
preserve

* Footnote 32
* Consequently, we observe an increase in chat volume in Part 2, especially in Decentralized
collapse (sum)words words_1 if treatment_id>0 & part<3, by(treatment_id economy_id part period) // economy-part level dataset
gen share_1=words_1/words
sum share_1 if period==5
gen share_1_p5=r(mean) // no one says anything during the second min of period 5
gen words_share=words
replace words_share=words/share_1_p5 if part==2
gen words_pt1=words_share if part==1
gen words_pt2=words_share if part==2
collapse (mean)words_pt*, by(treatment_id economy_id part) // economy-part level dataset
replace words_pt1=. if part==2
replace words_pt2=. if part==1 
collapse (mean)words_pt*, by(treatment_id economy_id) // economy-part level dataset
bysort treatment_id: signrank words_pt1=words_pt2
restore 
preserve
* we still detect significantly higher volumes in Decentralized than in Structured when pooling Parts 1 and 2 data
collapse (sum)words if treatment_id>0 & part>1 & high_gstar==0, by(treatment_id economy_id_n6 effort_stage) // economy-part level dataset
bysort effort_stage: ranksum words, by(treatment_id)

restore 
preserve

* Third, under $N=6$, we observe that the chat volume decreases with experience
replace part=4 if period>15
collapse (sum)words words_1 words_1_offer if part>2, by(treatment_id economy_id part period effort_stage) // economy-part level dataset
gen share_1=words_1/words
gen share_1_offer=words_1_offer/words
sum share_1 if period==18 & treatment_id==0
gen share_1_p18_base=r(mean)
sum share_1 if period==18 & treatment_id==1 & effort_stage==1
gen share_1_p18_dec=r(mean)
sum share_1 if period==18 & treatment_id==2 & effort_stage==1
gen share_1_p18_str=r(mean)
sum share_1_offer if period==18 & treatment_id==1 & effort_stage==0
gen share_1_offer_p18_dec=r(mean)
sum share_1_offer if period==18 & treatment_id==2 & effort_stage==0
gen share_1_offer_p18_str=r(mean)
gen words_share=words
replace words_share=words/share_1_p18_base if period>18 & effort_stage==1 & treatment_id==0
replace words_share=words/share_1_p18_dec  if period>18 & effort_stage==1 & treatment_id==1
replace words_share=words/share_1_p18_str  if period>18 & effort_stage==1 & treatment_id==2
replace words_share=words/share_1_offer_p18_dec  if period>18 & effort_stage==0 & treatment_id==1
replace words_share=words/share_1_offer_p18_str  if period>18 & effort_stage==0 & treatment_id==2
gen words_pt3=words_share if part==3
gen words_pt4=words_share if part==4
collapse (sum)words_pt*, by(treatment_id economy_id part period) // economy-part level dataset
replace words_pt3=. if part==4
replace words_pt4=. if part==3 
collapse (mean)words_pt*, by(treatment_id economy_id) // economy-part level dataset
bysort treatment_id : signrank words_pt3=words_pt4
bysort treatment_id : sum words_pt3 words_pt4

restore
preserve

replace part=4 if period>15

* Footnote 33
* Decentralized sticks out as having more chatting than the other treatments
collapse (mean)words if part>=3, by(treatment_id part economy_id team_id subject_id period effort_stage) // subject-period level dataset (original dataset is at chat string level)
collapse (sum)words, by(treatment_id part economy_id team_id subject_id period) // subject-period level dataset (original dataset is at chat string level)
collapse (sum)words, by(treatment_id part economy_id team_id period) // team-period level dataset
collapse (sum)words, by(treatment_id part economy_id period) // economy-period level dataset
collapse (sum)words, by(treatment_id part economy_id) // economy-part level dataset
ranksum words if treatment_id!=0 & part==3, by(treatment_id)
ranksum words if treatment_id!=0 & part==4, by(treatment_id)
restore
preserve
* teams with the highest ideal point debate less, as expected, since they should not make side-payments to others
gen words_low=words if high_gstar==0
gen words_hig=words if high_gstar==1
collapse (mean)words_low words_hig high_gstar if treatment_id==1 & part>1, by(economy_id_n6 team_id subject_id period effort_stage) // subject-period level dataset (original dataset is at chat string level)
collapse (sum)words_low words_hig (mean)high_gstar, by(economy_id_n6 team_id subject_id period effort_stage) // subject-period level dataset (original dataset is at chat string level)
collapse (sum)words_low words_hig (mean)high_gstar, by(economy_id_n6 team_id period effort_stage) // team-period level dataset
replace words_low=. if high_gstar==1
replace words_hig=. if high_gstar==0 
collapse (sum)words_low words_hig, by(economy_id_n6 period effort_stage) // economy-period level dataset
collapse (sum)words_low words_hig, by(economy_id_n6 effort_stage) // economy-part level dataset
signrank words_low=words_hig if effort_stage==0
signrank words_low=words_hig if effort_stage==1

* Chat content
use ..\data\coders_output.dta, clear

global cat_offer "confused_us confused_them coordination difficult prosocial antisocial overconfident disagree giveuppromises"
foreach v of varlist $cat_offer{
	replace `v'=`v'/rowtot
}
bysort team_id (period): gen economy_id_n6=economy_id[_N]
grstyle init 
grstyle set color #e41a1c #377eb8 #4daf4a #984ea3 #ff7f00 #ffff33 #999999 #f781bf #a65628
collapse $cat_offer, by(treatment_id part effort_stage economy_id_n6)

*Messages related to confusion, either within the team or from other teams, are few both in Decentralized and Structured (7\% and 3\%, respectively, when pooling Parts 2 and 3).
* These concerns are similar in economies of two and six (15\% vs. 13\% in Decentralized and 4\% vs. 8\% in Structured)
* Considerations originating from prosocial and antisocial preferences are uncommon (3\% and 1\%, respectively, when pooling Parts 1 and 2).
* This topic represents 33\% of the chat in Decentralized and 30\% in Structured.
graph pie $cat_offer if part>1 & effort_stage==0, by(treatment_id part) subtitle(, size(*0.9)) plabel(_all percent, format(%9.0f)) legend(r(3))

*The frequency is higher in economies of six than in economies of two, but it does not seem a major concern (6\% vs. 9\% in Decentralized and 1\% vs. 5\% in Structured)
graph pie $cat_offer if treatment_id==1 & part>1 & effort_stage==0, by(part) subtitle(, size(*0.9)) plabel(_all percent, format(%9.0f)) legend(r(3))
graph pie $cat_offer if treatment_id==2 & part>1 & effort_stage==0, by(part) subtitle(, size(*0.9)) plabel(_all percent, format(%9.0f)) legend(r(3))

*More present in the dialogue are the difficulties of predicting other teams' behavior and discussions of postponing offers to study the others' behavior (coordination category: 14\% in Decentralized and 6\% in Structured when pooling Parts 1 and 2).
graph pie $cat_offer if part>1 & effort_stage==0, by(treatment_id) subtitle(, size(*0.9)) plabel(_all percent, format(%9.0f)) legend(r(3))

* First, we study treatment differences in the distribution of mistakes and find that, while mistakes are few in all treatments (the median team made no mistakes), slightly more mistakes were made in Structured than Decentralized (on average 0.36 vs. 0.72, Wilcoxon-Mann-Whitney test: $p=0.143$, 36 obs. for Decentralized, 60 obs. for Structured). 
use ..\data\paper_data.dta, clear
drop if treatment_id==3
keep if period==1
gen mistakes=mistakes_pt1+mistakes_pt2+mistakes_pt3
bysort team_id (subject_id period): gen rank=_n
bysort treatment_id: sum mistakes if rank==1, det
ranksum mistakes if rank==1 & treatment_id!=0, by(treatment_id)
